<?php
// +----------------------------------------------------------------------
// | [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 洗车
// +----------------------------------------------------------------------
namespace app\ymkj\controller;

use think\facade\Session;
use think\facade\Config;
use think\Controller;
use think\Db;

class Payapi extends Controller
{
  public function returl()
  {
    $data = input();
    $order = Db::name('order')->where('id','=',$data['reqsn'])->find();
    // 检测支付状态为未支付
    if($order['status'] == 0) {
      $this->updateOrderStatus($order['id']);
      if ($order['type'] == 'more') {
        $start_time = time();
        $end_time = time()+365*24*60*60;
        $this->fenxiao($order['jine'],$order['openid']);
        $result = Db::name('user')
          ->where('openid','=',$order['openid'])
          ->update(['vip'=>1,'bolishui'=>12,'start_time' =>$start_time,'end_time'=>$end_time]);
      }
    }
    $this->success('消费成功',url("user/index"));
  }

  // 更新支付状态
  private function updateOrderStatus($orderID)
  {
    Db::name('order')->where('id','=',$orderID)->update(['status'=>1]);
  }
  
  // 分销
  public function fenxiao($jine,$user)
  {
    $fenxiao = Db::name('fenxiao')->where('fenxiao','=','fenxiao')->find();
    $wxuser = Db::name('user')->where('openid','=',$user)->find();
    if($fenxiao['status'] == 1 && $fenxiao['type'] == 1){ // 百分比分销
      $f1 = Db::name('user')->where('openid','=',$wxuser['f1'])->setInc('balance',round($jine*$fenxiao['f1']/100,2));
      $f2 = Db::name('user')->where('openid','=',$wxuser['f2'])->setInc('balance',round($jine*$fenxiao['f2']/100,2));
      $f3 = Db::name('user')->where('openid','=',$wxuser['f3'])->setInc('balance',round($jine*$fenxiao['f3']/100,2));
    }
    if($fenxiao['status'] == 1 && $fenxiao['type'] == 2){ // 固定金额
      $f1 = Db::name('user')->where('openid','=',$wxuser['f1'])->setInc('balance',$fenxiao['f1']);
      $f2 = Db::name('user')->where('openid','=',$wxuser['f2'])->setInc('balance',$fenxiao['f2']);
      $f3 = Db::name('user')->where('openid','=',$wxuser['f3'])->setInc('balance',$fenxiao['f3']);
    }
  }

  
}
?>